//
//  DHLoginViewController.m
//  Dresshere
//
//  Created by 张朋 on 2017/12/19.
//  Copyright © 2017年 张朋. All rights reserved.
//

#import "DHLoginViewController.h"
#import "DHLoginInfoViewController.h"
#import "DHLoginStyleViewController.h"
#import <JPush/JPUSHService.h>

@interface DHLoginViewController ()

@property (weak, nonatomic) IBOutlet UIButton *ensureButton;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topLayout;
@property (weak, nonatomic) IBOutlet UITextField *phoneTF;
@property (weak, nonatomic) IBOutlet UIView *phoneView;
@property (weak, nonatomic) IBOutlet UITextField *codeTF;
@property (weak, nonatomic) IBOutlet UIView *codeView;
@property (weak, nonatomic) IBOutlet UIButton *codeButton;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topLabelLayout;

@end

@implementation DHLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    if (iPhoneX) {
        self.topLabelLayout.constant = 48;
    }

    self.ensureButton.layer.cornerRadius = self.ensureButton.height / 2;
    self.codeButton.layer.borderWidth = 1;
    self.codeButton.layer.borderColor = [RGBAlpha(76, 77, 92, 0.5) CGColor];
    
    [self buttonActions];
}

- (void)buttonActions{
    [self.codeButton bk_addEventHandler:^(id sender) {
        if ([self.phoneTF.text isValidPhone]) {
            [[DHNetAPIClient shareDHNetAPIClient] request_smscodeWithPhone:self.phoneTF.text AndBlock:^(id data, NSError *error) {
                __block int leftTime = 60;
                self.codeButton.backgroundColor = [UIColor colorWithHexString:@"#cccccc"];
                [self.codeButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                [self.codeButton setTitle:[NSString stringWithFormat:@"已发送(%is)", leftTime] forState:UIControlStateDisabled];
                self.codeButton.layer.borderColor = [UIColor colorWithHexString:@"#cccccc"].CGColor;
                self.codeButton.enabled = NO;
                [NSTimer bk_scheduledTimerWithTimeInterval:1 block:^(NSTimer *timer) {
                    if (leftTime == 0) {
                        self.codeButton.enabled = YES;
                        [self.codeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
                        [self.codeButton setTitleColor:[UIColor colorWithHexString:@"#4a4a4a"] forState:UIControlStateNormal];
                        self.codeButton.backgroundColor = [UIColor colorWithHexString:@"#f2f2f2"];
                        //计时完成，销毁定时器
                        [timer invalidate];
                    }
                    leftTime--;
                    [sender setTitle:[NSString stringWithFormat:@"已发送(%is)", leftTime] forState:UIControlStateDisabled];
                } repeats:YES];
            }];
        }else{
            [NSObject showHudTipStr:@"您输入的手机号码格式有错误"];
        }
    } forControlEvents:UIControlEventTouchUpInside];
    
    [self.ensureButton bk_addEventHandler:^(id sender) {
        if ([self.phoneTF.text isValidPhone] && self.codeTF.text.length > 0) {
            [[DHNetAPIClient shareDHNetAPIClient] request_loginWithPhone:self.phoneTF.text code:self.codeTF.text andBlock:^(id data, NSError *error) {
                if (data) {
                    User *user = [User modelWithJSON:[data objectForKey:@"user"]];
                    if (user) {
                        user.status = [NSNumber numberWithInt:1];
                        user.token = [data objectForKey:@"token"];
                        [Login doLogin:user];
                        [JPUSHService setTags:[NSSet setWithObject:@"IOS"] aliasInbackground:user._id];
                        [[GlobalManager shareGlobalManager] refreshBasicInfoAndBlock:^(id data, NSError *error) {
                            
                        }];
                        [[GlobalManager shareGlobalManager] refreshVIPInfoAndBlock:^(id data, NSError *error) {
                            
                        }];
                        NSString *sexString = [[NSUserDefaults standardUserDefaults] objectForKey:USER_SELECT_SEX];
                        if (sexString) {
                            [[DHNetAPIClient shareDHNetAPIClient] request_user_sexWithParams:@{@"sex":sexString} andBlock:^(id data, NSError *error) {
                                [kAppDelegate configHomeController];
                            }];
                        }else{
                            [kAppDelegate configHomeController];
                        }
                    }
                }
            }];
        }else{
            [NSObject showHudTipStr:@"请输入手机号码"];
        }
    } forControlEvents:UIControlEventTouchUpInside];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [super touchesBegan:touches withEvent:event];
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
